package demo.json.jackson.databind;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import demo.java.time.DateTimeDemo;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 该类可以将data转换成long类型
 */
public class Date2StringSerizlizer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
            throws IOException {
        LocalDateTime dateTime = DateTimeDemo.toLocalDateTime(date.getTime());
        jsonGenerator.writeString(dateTime.toString());
        // 将毫秒值转换成秒变成long型数据返回
        // jsonGenerator.writeNumber(date.getTime() / 1000);
    }
}
